﻿using System.Linq;
using System.Text;
using System.Text.Json.Serialization;

namespace TableTools.Models
{
    public class TableTemplate
    {
        public const string CacheFileName = "tables.json";

        [JsonPropertyName("index")]
        public int Index { get; set; }

        [JsonPropertyName("name")]
        public string? Name { get; set; }

        [JsonPropertyName("jsonCode")]
        public string? JsonCode { get; set; }

        [JsonPropertyName("markdown")]
        public string? Markdown { get; set; }

        [JsonPropertyName("table")]
        public TableInfo Table { get; set; }
    }

    public class TableInfo
    {

        [JsonPropertyName("row")]
        public int Row { get; set; }

        [JsonPropertyName("col")]
        public int Col { get; set; }

        [JsonPropertyName("headers")]
        public string[] Headers { get; set; }

        [JsonPropertyName("rows")]
        public string[][] Rows { get; set; }

        public TableInfo()
        {

        }

        public TableInfo(int row, int col)
        {
            Row = row;
            Col = col;
            Headers = new string[col];
            Rows = new string[row][];
            for (int i = 0; i < row; i++)
            {
                Rows[i] = new string[col];
            }
        }

        public string ToMarkdown()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(string.Join("|", Headers));
            foreach (var row in Rows) 
            {
                sb.AppendLine(string.Join("|", row));
            }

            return sb.ToString();
        }
    }
}
